﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web;
using System.Web.Routing;
using System.Linq.Expressions;
using System.Configuration;
using System.IO;
using System.Xml;
using RedSand.Web.Config;
using RedSand.Web.Language;

namespace RedSand.Web.Mvc.Controls
{
    public static class RsLanguage
    {

        /// <summary>
        /// Chuyển ngôn ngữ với "resource tag" = "propertise name" đồng thời sinh thẻ label
        /// </summary>
        /// <typeparam name="TModel">Model class</typeparam>
        /// <typeparam name="TValue">Value type</typeparam>
        /// <param name="helper">Html</param>
        /// <param name="expression">Lambda</param>
        /// <returns></returns>
        public static MvcHtmlString TranslateFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
        {
            return TranslateFor<TModel, TValue>(helper, expression, "Global", null);
        }
        /// <summary>
        /// Chuyển ngôn ngữ với "resource tag" = "propertise name" đồng thời sinh thẻ label
        /// </summary>
        /// <typeparam name="TModel">Model class</typeparam>
        /// <typeparam name="TValue">Value type</typeparam>
        /// <param name="helper">Html</param>
        /// <param name="expression">Lambda</param>
        /// <param name="resourceGroup">ResourceGroup</param>
        /// <returns></returns>
        public static MvcHtmlString TranslateFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string resourceGroup)
        {
            return TranslateFor<TModel, TValue>(helper, expression, resourceGroup, null);
        }
        /// <summary>
        /// Chuyển ngôn ngữ với "resource tag" = "propertise name" đồng thời sinh thẻ label
        /// </summary>
        /// <typeparam name="TModel">Model class</typeparam>
        /// <typeparam name="TValue">Value type</typeparam>
        /// <param name="helper">Html</param>
        /// <param name="expression">Lambda</param>
        /// <param name="resourceGroup">Nhóm từ khóa</param>
        /// <param name="htmlAttributes">Thuộc tính html</param>
        /// <returns></returns>
        public static MvcHtmlString TranslateFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string resourceGroup, object htmlAttributes)
        {
            if (expression.Body is MemberExpression)
            {
                return Translate(helper, (expression.Body as MemberExpression).Member.Name, resourceGroup, "label", htmlAttributes);
            }
            throw new Exception("Expression body is not MemberExpression");
        }

        /// <summary>
        /// Chuyển ngôn ngữ từ resource bởi resource-tag
        /// </summary>
        /// <param name="helper">Html</param>
        /// <param name="resourceTag">ResourceTag</param>
        /// <returns></returns>
        public static MvcHtmlString Translate(this HtmlHelper helper, string resourceTag)
        {
            return Translate(helper, resourceTag, "Global");
        }
        /// <summary>
        /// Chuyển ngôn ngữ từ resource bởi resource-tag
        /// </summary>
        /// <param name="helper">Html</param>
        /// <param name="resourceTag">ResourceTag</param>
        /// <param name="resourceGroup">ResourceGroup</param>
        /// <returns></returns>
        public static MvcHtmlString Translate(this HtmlHelper helper, string resourceTag, string resourceGroup)
        {
            return MvcHtmlString.Create(Language.RsLanguage.Translate(resourceTag, resourceGroup));
        }
        /// <summary>
        /// Chuyển ngôn ngữ từ resource bởi resource-tag đồng thời sinh thẻ html 
        /// </summary>
        /// <param name="helper">Html</param>
        /// <param name="resourceTag">ResourceTag</param>
        /// <param name="resourceGroup">ResourceGroup</param>
        /// <param name="htmlTag">Tên thẻ html</param>
        /// <returns></returns>
        public static MvcHtmlString Translate(this HtmlHelper helper, string resourceTag, string resourceGroup, string htmlTag)
        {
            string text = Language.RsLanguage.Translate(resourceTag, resourceGroup);
            TagBuilder tag = new TagBuilder(htmlTag);
            tag.InnerHtml = text;
            return MvcHtmlString.Create(tag.ToString());
        }
        /// <summary>
        /// Chuyển ngôn ngữ từ resource bởi resource-tag đồng thời sinh thẻ html 
        /// </summary>
        /// <param name="helper">Html</param>
        /// <param name="resourceTag">ResourceTag</param>
        /// <param name="resourceGroup">ResourceGroup</param>
        /// <param name="htmlTag">Tên thẻ html</param>
        /// <param name="htmlAttributes">Thuộc tính thẻ html</param>
        /// <returns></returns>
        public static MvcHtmlString Translate(this HtmlHelper helper, string resourceTag, string resourceGroup, string htmlTag, object htmlAttributes)
        {
            string text = Language.RsLanguage.Translate(resourceTag, resourceGroup);
            TagBuilder tag = new TagBuilder(htmlTag);
            tag.InnerHtml=text;
            tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(tag.ToString());
        }

    }
}
